import Dialog from "../../../component/vant/dialog/dialog";

let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        spinShow: false,
        productId: '',
        product: {},
        dynamicCargoData: [],
        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '复制', type: "copy"},
            {name: '撤销', type: "cancel"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let productId = options.productId
        this.data.productId = productId
        if (productId && productId != null) {
            this.queryData(productId)
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    onShow: function () {

    },
    queryData: function (productId) {
        let _this = this
        app.formGet('/app/product/getProductDetail?productId=' + productId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    product: res.data.product,
                    dynamicCargoData: res.data.detailList
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    toHomePage: function () {
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/product/add/productAdd"
            })
        } else if (type == 'copy') {
            wx.redirectTo({
                url: "/pages/product/add/productAdd?copyProductId=" + this.data.productId
            })
        } else if (type == 'cancel') {
            this.handleProductCancel()
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    handleProductCancel: function () {
        let _this = this
        Dialog.confirm({
            title: '温馨提示',
            message: '确定要撤销吗?',
        })
            .then(() => {
                let data = {}
                data.productId = this.data.productId
                app.formPost('/app/product/cancel', data).then(res => {
                    wx.hideLoading()
                    if (res.code === 0) {
                        app.toast(res.message, "success")
                        setTimeout(function () {
                            wx.redirectTo({
                                url: "/pages/product/index/index"
                            })
                        }, 2000)
                    } else {
                        app.toast(res.message, "fail")
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                console.log("concel")
                // on cancel
            });
    },
    btnEdit: function () {
        wx.navigateTo({
            url: "/pages/product/edit/productEdit?productId=" + this.data.productId
        })
    }
})
